#include "MainGameState.h"
#include "Game.h"
#include "Graphics.h"
#include "CameraManager.h"
#include "DirectInput.h"

const char* GameState::NAME = "MainGameState";

MainGameState::MainGameState()
{
	static bool Registered = TheGame::Instance()->AddGameState(MainGameState::NAME, this);
}

MainGameState::~MainGameState()
{
}

void MainGameState::OnActive()
{	

	TheCameraManager::Instance()->SetCamera("Camera_Debug");

	TheCameraManager::Instance()->BuildCameraProjectionMatrix(D3DX_PI * 0.25f, 800/600, 1.0f, 10000.0f);
	TheCameraManager::Instance()->GetActiveCamera()->LookAt(Vec3(0.0f,0.0f,-10.0f),Vec3(0.0f,0.0f,0.0f),Vec3(0.0f,1.0f,0.0f));

	// Create the model object.
	m_Model = new Model;

	m_Light = new Light;

	// Initialize the model object.
	m_Model->Initialize();

	m_Light->Init(1,g_Green,Vec3(0.0f,0.0f,1.0f),Vec3(0.0f,0.0f,0.0f));
	
	// Create the color shader object.
	//m_ColorShader = new ColorShader;

	m_TextureShader = new TextureShader;

	m_LightShader = new LightShader;

	// Initialize the color shader object.
	//m_ColorShader->Initialize();

	m_TextureShader->Initialize();

	m_LightShader->Initialize();

	TheInputDevice::Instance()->InitKeyboard();
	TheInputDevice::Instance()->InitMouse();

	m_ObjMesh.Load("cup.obj");

}

void MainGameState::Update()
{
	TheInputDevice::Instance()->Update();
	TheCameraManager::Instance()->Update();
}

void MainGameState::Draw()
{
	TheGraphics::Instance()->GetD3DObj()->BeginScene(1.0f,1.0f,1.0f,1.0f);

	// Initialize the model object.
	m_Model->Render();
	// Create the color shader object.
	/*m_ColorShader->Render(m_Model->GetIndexCount(),
		TheCameraManager::Instance()->GetActiveCamera()->GetWorldMatrix(),
		TheCameraManager::Instance()->GetActiveCamera()->GetViewMatrix(),
		TheCameraManager::Instance()->GetActiveCamera()->GetProjectionMatrix());*/

	/*m_TextureShader->Render(m_Model->GetIndexCount(),
		TheCameraManager::Instance()->GetActiveCamera()->GetWorldMatrix(),
		TheCameraManager::Instance()->GetActiveCamera()->GetViewMatrix(),
		TheCameraManager::Instance()->GetActiveCamera()->GetProjectionMatrix(),
		m_Model->GetTexture());*/

	static float rotation = 0.0f;

	// Update the rotation variable each frame.
	rotation += (float)D3DX_PI * 0.01f;
	if(rotation > 360.0f)
	{
		rotation -= 360.0f;
	}

	float x = sin(rotation);
	float z = cos(rotation);
	
	Vec3 pos(x,0.0,z);

	// Rotate the world matrix by the rotation value so that the triangle will spin.
	static D3DXMATRIX mat, mat2;
	D3DXMatrixRotationY(&mat, rotation);

	D3DXMatrixTranslation(&mat2, rotation,0.0f,0.0f);

	m_LightShader->Render(m_Model->GetIndexCount(),
		TheCameraManager::Instance()->GetActiveCamera()->GetWorldMatrix(),
		TheCameraManager::Instance()->GetActiveCamera()->GetViewMatrix(),
		TheCameraManager::Instance()->GetActiveCamera()->GetProjectionMatrix(),
		m_Model->GetTexture(),pos, m_Light->GetDiffuse());

//	m_ObjMesh.Render();

	TheGraphics::Instance()->GetD3DObj()->EndScene();
}	

void MainGameState::OnDeActive()
{
}
